package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.zhaoshang.ZsProject;
import com.yanqu.road.entity.zhaoshang.enums.eProjectStateType;
import com.yanqu.road.logic.pb.ZhaoShangPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.zhaoshang.ZsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.List;

@Cmd(code = CrossProtocol.C2_ZHAO_SHANG_PROJECT_SEARCH_WHERE, desc = "查询玩家正参与的项目")
public class ZsProjectSearchWhereIsEnemyCmd extends CrossCommand {

    private long searchUserId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.ZsProjectSearchEnemyWhereReqMsg reqMsg = ZhaoShangProto.ZsProjectSearchEnemyWhereReqMsg.parseFrom(packet.getBytes());
        searchUserId = reqMsg.getUserId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        List<ZsProject> projectList = ZsMgr.getUserJoinProject(serverId, searchUserId, eProjectStateType.InProgress.getIntValue());
        if(projectList.size() == 0){
            ZhaoShangProto.ZsProjectSearchEnemyWhereRespMsg.Builder respMsg = ZhaoShangProto.ZsProjectSearchEnemyWhereRespMsg.newBuilder();
            respMsg.setRet(GameErrorCode.E_ZS_PROJECT_USER_NOT_JOIN_PROJECT);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_ZS_PROJECT_SEARCH_WHERE_IS_ENEMY, respMsg));
        }else {
            ZhaoShangProto.ZsProjectSearchEnemyWhereRespMsg.Builder respMsg = ZhaoShangProto.ZsProjectSearchEnemyWhereRespMsg.newBuilder();
            for(ZsProject project : projectList){
                respMsg.addProject(ZhaoShangPb.parseZsProjectInfoTemp(project, true, packet.getPlayerId()));
            }
            respMsg.setRet(0);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_ZS_PROJECT_SEARCH_WHERE_IS_ENEMY, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
